<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">.ajaxSuccess()</h1>
<div class="entry-meta">
						所属分类：<span class="category"><a href="/category/ajax/">Ajax</a> &gt; <a href="/category/ajax/global-ajax-event-handlers/">全局 Ajax 事件处理器</a></span>
<span class="pull-right">英文文档：<a href="https://api.jquery123.com/ajaxSuccess/" target="_blank">.ajaxSuccess()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
<article class="entry method" id="ajaxSuccess1"><h2 class="section-title">
<span class="name">.ajaxSuccess( handler(event, XMLHttpRequest, ajaxOptions) )</span><span class="returns">返回: <a href="/Types/#jQuery">jQuery</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>绑定一个函数当 Ajax 请求成功完成时执行。 这是一个<a href="/Ajax_Events">Ajax Event</a>.</p>
<ul class="signatures"><li class="signature" id="ajaxSuccess-handlerevent--XMLHttpRequest--ajaxOptions">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.0/">1.0</a></span>.ajaxSuccess( handler(event, XMLHttpRequest, ajaxOptions) )</h4>
<ul><li>
<div><strong>handler(event, XMLHttpRequest, ajaxOptions)</strong></div>
<div>类型: <a href="/Types/#Function">Function</a>()</div>
<div>被调用的函数。</div>
</li></ul>
</li></ul>
<div class="longdesc" id="entry-longdesc">
<p>每当一个Ajax请求成功完成，jQuery就会触发<code>ajaxSuccess</code>事件，在这个时间点所有处理函数都会使用<code>.ajaxSuccess()</code>方法注册并执行。</p>
<p>观察这种方法，建立一个基本的Ajax加载请求：</p>
<div class="syntaxhighlighter xml nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span> <span class="attribute">class</span>=<span class="value">"trigger"</span>&gt;</span>Trigger<span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span> <span class="attribute">class</span>=<span class="value">"result"</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span> <span class="attribute">class</span>=<span class="value">"log"</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>在document任何元素上绑定事件处理器：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$(document).ajaxSuccess(<span class="keyword">function</span>() {</code></div></div><div class="container"><div class="line"><code>  $( <span class="string">".log"</span> ).text( <span class="string">"Triggered ajaxSuccess handler."</span> );</code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>现在，我们可以使用任何的jQuery方法构建一个Ajax请求：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$( <span class="string">".trigger"</span> ).on(<span class="string">"click"</span>, <span class="keyword">function</span>() {</code></div></div><div class="container"><div class="line"><code>  $( <span class="string">".result"</span> ).load( <span class="string">"ajax/test.html"</span> );</code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>当我们点击class为 <code>trigger</code> 的元素并且Ajax请求成功完成，这个信息就会显示。</p>
<p><strong>但是从 jQuery 1.8 开始, <code>.ajaxStart()</code> 方法只能绑定到 <code>document</code>元素.</strong></p>
<p>无论哪一个Ajax请求被完成，所有<code>ajaxSuccess</code>处理器都将被执行。如果我们必须区分不同的请求,我们可以使参数传递给这个处理器。 他是通过事件对象、 <code>XMLHttpRequest</code> 对象和设置对象中使用的请求，做每一次<code>ajaxSuccess</code> 处理器执行的。 举个例子,我们能限制我们的回调到只处理事件处理某一特定的URL:</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$(document).ajaxSuccess(<span class="keyword">function</span>(event, xhr, settings) {</code></div></div><div class="container"><div class="line"><code>  <span class="keyword">if</span> ( settings.url == <span class="string">"ajax/test.html"</span> ) {</code></div></div><div class="container"><div class="line"><code>    $( <span class="string">".log"</span> ).text( <span class="string">"Triggered ajaxSuccess handler. The ajax response was: "</span> +</code></div></div><div class="container"><div class="line"><code>                      xhr.responseText );</code></div></div><div class="container"><div class="line"><code>  }</code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p><strong>注意:</strong> 你可以得到返回的AJAX内容
			察看XML和HTML的<code>xhr.responseXML</code> 或 <code>xhr.responseHTML</code> 之间的分别。</p>
</div>
<h3>其他注意事项：</h3>
<div class="longdesc">
<ul>
<li> 
			                如果<code>global</code>选项设置为<code>false</code>，调用<code><a href="/jQuery.Ajax">$.ajax()</a></code> 或 <code><a href="/jQuery.ajaxSuccess">$.ajaxSuccess()</a></code>方法将不会被触发。</li>
</ul>
</div>
<section class="entry-examples" id="entry-examples"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-0">
<h4><span class="desc">当Ajax请求成功完成时，显示一个信息。</span></h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$(document).ajaxSuccess(<span class="keyword">function</span>(event, request, settings) {</code></div></div><div class="container"><div class="line"><code>      $( <span class="string">"#msg"</span> ).append( <span class="string">"&lt;li&gt;Successful Request!&lt;/li&gt;"</span> );</code></div></div><div class="container"><div class="line"><code>      });</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div></section>
</div></article> </div>
</article>